package com.yellowpage.bigdata.service;

import java.util.List;
import java.util.Map;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;

import com.yellowpage.bigdata.data.NspOnofflogTaRegionMonthEntity;
import com.yellowpage.bigdata.dao.NspOnofflogTaRegionMonthEntityDao;

public class NspOnofflogTaRegionMonthEntityServiceImpl extends BaseServiceImpl implements NspOnofflogTaRegionMonthEntityService {
    private NspOnofflogTaRegionMonthEntityDao nspOnofflogTaRegionMonthEntityDao;

    public void setNspOnofflogTaRegionMonthEntityDao(NspOnofflogTaRegionMonthEntityDao nspOnofflogTaRegionMonthEntityDao) {
        this.nspOnofflogTaRegionMonthEntityDao = nspOnofflogTaRegionMonthEntityDao;
    }


    /* Smooth generate Method */
    public NspOnofflogTaRegionMonthEntity getNspOnofflogTaRegionMonthEntity(Long id) {
        return nspOnofflogTaRegionMonthEntityDao.get(id);
    }

    public List<NspOnofflogTaRegionMonthEntity> getAllNspOnofflogTaRegionMonthEntity() {
        return nspOnofflogTaRegionMonthEntityDao.getAll();
    }

    public List<Map> queryNspOnofflogTaRegionMonthEntityByPage(Map queryMap, PageRender pageRender) {
        List<Map> resultList = getQueryAssistantSQL(queryMap).queryByPage(
                baseDao.getConnection(), queryMap, pageRender);

        return resultList;
    }


    public void saveNspOnofflogTaRegionMonthEntity(NspOnofflogTaRegionMonthEntity nspOnofflogTaRegionMonthEntity) {
        nspOnofflogTaRegionMonthEntityDao.save(nspOnofflogTaRegionMonthEntity);
    }

    public void updateNspOnofflogTaRegionMonthEntity(NspOnofflogTaRegionMonthEntity nspOnofflogTaRegionMonthEntity) {
        nspOnofflogTaRegionMonthEntityDao.update(nspOnofflogTaRegionMonthEntity);
    }

    public void delete(List deleteList) {
        nspOnofflogTaRegionMonthEntityDao.remove(deleteList);
    }

    @Override
    public List<Map> queryAllNspOnofflogTaRegionDayEntity(Map queryMap) {
        List<Map> resultList = getQueryAssistantSQL(queryMap).query(
                baseDao.getConnection(), queryMap);

        return resultList;
    }

    /**
     * 图表汇总查询
     * */
    @Override
    public List<Map> queryAllNspOnofflogTaRegionMonthSumByDate(Map queryMap) {
        QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
                "SELECT r.SHORT_NAME as REGION_CODE , sum(a.IMEI_NEW_COUNT) as IMEI_NEW_COUNT FROM NSP_ONOFFLOG_TA_REGION_MONTH a,OMC_REGION r WHERE r.REGION_CODE=a.REGION_CODE and 1=1 " + getQueryString(queryMap));
        sqlAssistant.setTail("group by r.SHORT_NAME order by r.SHORT_NAME desc");
        List<Map> resultList = sqlAssistant.query(baseDao.getConnection(), queryMap);
        return resultList;
    }

    /**
     * 分页查询
     * */
    private QueryAssistantSQL getQueryAssistantSQL(Map queryMap) {
        QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
                "SELECT a.ID , a.LOG_DATE , r.SHORT_NAME as REGION_CODE, a.IMEI_MONTH_COUNT , a.ACTIVE_COUNT , a.PASSIVE_COUNT , a.IMEI_ALL_COUNT , a.IMEI_NEW_COUNT , a.CREATE_USER , a.CREATE_TIME , a.LAST_UPDATE_USER , a.LAST_UPDATE_TIME , a.NOTE    FROM NSP_ONOFFLOG_TA_REGION_MONTH a,OMC_REGION r WHERE r.REGION_CODE=a.REGION_CODE and 1=1 " + getQueryString(queryMap));
        return sqlAssistant;
    }

    private String getQueryString(Map queryMap){
        String startDate = queryMap.get("logDate").equals("") ? "" : " and LOG_DATE >= " + queryMap.get("logDate");
        String endDate = queryMap.get("query_logDate_end").equals("") ? "" : " and LOG_DATE <= " + queryMap.get("query_logDate_end");
        String regions = queryMap.get("regionCode").equals("()") ? "" : " and r.SHORT_NAME in " + queryMap.get("regionCode");
        return startDate + endDate + regions;

    }

}	